package com.im.service.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import com.im.service.common.Result;
import com.im.service.domain.entity.ImGroup;
import com.im.service.domain.entity.ImGroupUser;
import com.im.service.domain.entity.ImUser;
import com.im.service.domain.vo.ImGroupUserVO;
import com.im.service.domain.vo.ImGroupVO;
import com.im.service.mapper.ImGroupMapper;
import com.im.service.mapper.ImUserMapper;
import com.im.service.service.ImGroupService;
import com.im.service.service.ImGroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * im-群组 服务实现类
 * </p>
 *
 * @author 刘志强
 * @since 2024-07-03
 */
@Service
public class ImGroupServiceImpl extends ServiceImpl<ImGroupMapper, ImGroup> implements ImGroupService {

    @Autowired
    private ImGroupUserService groupUserService;
    @Autowired
    private ImUserMapper userMapper;
    @Value("${avatarUrl}")
    private String avatarUrl;

    @Override
    public Result<ImGroup> createGroup(ImGroup imGroup) {
        imGroup.setId(null);
        imGroup.setUserId(StpUtil.getLoginIdAsLong());
        imGroup.setAvatar(avatarUrl + "group.png");
        this.save(imGroup);
        groupUserService.addGroupUser(imGroup, imGroup.getUserId(), 1);
        return Result.okData(imGroup);
    }

    @Override
    public Result dissolveGroup(Long id) {
        ImGroup imGroup = this.getById(id);
        List<Long> groupUserIdList = SimpleQuery.list(Wrappers.lambdaQuery(ImGroupUser.class)
                .eq(ImGroupUser::getGroupId, id), ImGroupUser::getId
        );
        for (Long groupUserId : groupUserIdList) {
            groupUserService.removeGroupUser(imGroup, groupUserId, 1);
        }
        this.removeById(id);
        return Result.ok();
    }

    @Override
    public Result<ImGroupVO> groupInfo(Long groupId) {
        ImGroup imGroup = this.getById(groupId);
        ImGroupVO groupVO = BeanUtil.toBean(imGroup, ImGroupVO.class);
        List<ImGroupUser> imGroupUsers = groupUserService.list(Wrappers.lambdaQuery(ImGroupUser.class)
                .eq(ImGroupUser::getGroupId, groupId)
        );
        List<ImGroupUserVO> groupUserList = imGroupUsers.stream().map(imGroupUser -> {
            ImGroupUserVO imGroupUserVO = BeanUtil.toBean(imGroupUser, ImGroupUserVO.class);
            ImUser imUser = userMapper.selectById(imGroupUser.getUserId());
            imGroupUserVO.setUserNickname(imUser.getNickname());
            imGroupUserVO.setAvatar(imUser.getAvatar());
            return imGroupUserVO;

        }).collect(Collectors.toList());
        groupVO.setGroupUserList(groupUserList);
        return Result.okData(groupVO);
    }
}
